home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_10_11
/
1011099a
< prev
next >
Wrap
Text File
|
1992-08-31
|
19KB
|
554 lines
/***********************************************
*
* file d:\cips\scale.c
*
* Functions: This file contains
* zoom_image_array
* shrink_image_array
* interpolate_pixel
* average_pixel
* median_pixel
* get_scaling_options
* blank_image_array
*
* Purpose:
* These functions implement image array
* zooming (enlarging) and shrinking.
*
* External Calls:
* wtiff.c - does_not_exist
* round_off_image_size
* create_allocate_tiff_file
* write_array_into_tiff_image
* tiff.c - read_tiff_header
* rtiff.c - read_tiff_image
* rstring.c - read_string
* numcvrt.c - get_integer
* filter.c - median_of
*
* Modifications:
* 8 April 1992 - created
*
*************************************************/
#include "d:\cips\cips.h"
#include <malloc.h>
/*******************************************
*
* zoom_image_array(...
*
* This function zooms in on an input
* image array. It zooms by enlarging
* an input image array and writing the
* resulting image arrays to an output
* image file. It can zoom or enlarge by
* a factor of 2 or 4.
*
* You can zoom or enlarge an image array
* by using either the replication or
* interpolation methods.
*
*******************************************/
zoom_image_array(in_name, out_name, the_image, out_image,
il, ie, ll, le, scale, method)
char in_name[], out_name[], method[];
int il, ie, ll, le, scale;
short the_image[ROWS][COLS],
out_image[ROWS][COLS];
{
int A, B, a, b, count, factor, i, j, length, width;
struct tiff_header_struct image_header;
/*******************************************
*
* Check the scale factor. If it is not
* a valid factor (2 or 4), then set
* it to 2.
*
*******************************************/
factor = scale;
if(factor != 2 &&
factor != 4) factor = 2;
if(does_not_exist(out_name)){
printf("\n\n output file does not exist %s", out_name);
read_tiff_header(in_name, &image_header);
image_header.image_length = ROWS*factor;
image_header.image_width = COLS*factor;
create_allocate_tiff_file(out_name, &image_header,
out_image);
} /* ends if does_not_exist */
/*******************************************
*
* Let me try to explain the nested loops
* shown below.
*
* We will enlarge the_image by the factor.
* Therefore, we want to divide the_image
* into parts of size ROWS/factor by
* COLS/factor. We will loop through
* the_image parts ROWS/factor and COLS/factor
* using the loops over i and j.
*
* We divided the_image into parts so we must
* include all of these parts. That is why we
* do the loops over A and B. There are
* factor*factor parts.
*
* Finally, we do the loops over a and b.
* We must replicate every element in the_image
* factor*factor times and put these into
* out_image. The a and b loops perform this
* task.
*
* The equations inside the []'s for
* the_image and out_image are also confusing.
* If you work them out, you'll see that we
* are bouncing through the image arrays
* and fitting the pixels into the right
* places.
*
* The final proof is that this works.
*
* One final note: the factor should divide
* evenly into ROWS. For example, ROWS=100
* so using a factor of 8 is not good.
* 100/8 = 12.5 and this leave you with
* an empty strip along the right and
* bottom edges of the out_image.
*
*******************************************/
/*******************************************
*
* Replication method
*
*******************************************/
if(method[0] == 'r' || method[0] == 'R'){
read_tiff_image(in_name, the_image, il, ie, ll, le);
count = 1;
for(A=0; A<factor; A++){
for(B=0; B<factor; B++){
for(i=0; i<ROWS/factor; i++){
for(j=0; j<COLS/factor; j++){
for(a=0; a<factor; a++){
for(b=0; b<factor; b++){
out_image[factor*i+a][factor*j+b] =
the_image[i+A*ROWS/factor][j+B*COLS/factor];
} /* ends loop over b */
} /* ends loop over a */
} /* ends loop over j */
} /* ends loop over i */
printf("\nzooming replication %3d of %3d",
count++, factor*factor);
write_array_into_tiff_image(out_name, out_image,
1+A*ROWS, 1+B*COLS, 101+A*ROWS, 101+B*COLS);
} /* ends loop over B */
} /* ends loop over A */
} /* ends replication method */
/***************************
*
* Interpolation method
*
****************************/
if(method[0] == 'i' || method[0] == 'I'){
read_tiff_image(in_name, the_image,
il, ie, ll, le);
count = 1;
for(A=0; A<factor; A++){
for(B=0; B<factor; B++){
for(i=0; i<ROWS/factor; i++){
for(j=0; j<COLS/factor; j++){
for(a=0; a<factor; a++){
for(b=0; b<factor; b++){
out_image[factor*i+a][factor*j+b] =
interpolate_pixel(the_image, A, B,
i, j, a, b, factor);
} /* ends loop over b */
} /* ends loop over a */
} /* ends loop over j */
} /* ends loop over i */
printf("\nzooming interpolation %3d of %3d",
count++, factor*factor);
write_array_into_tiff_image(out_name, out_image,
1+A*ROWS, 1+B*COLS,
101+A*ROWS, 101+B*COLS);
} /* ends loop over B */
} /* ends loop over A */
} /* ends interpolation method */
} /* ends zoom_image_array */
/***********************************************
*
* interpolate_pixel(...
*
* This function interpolates between pixel
* values and returns the interlopated value.
*
***********************************************/
interpolate_pixel(the_image, A, B, i, j, a, b, factor)
int A, B, a, b, factor, i, j;
short the_image[ROWS][COLS];
{
int num, x = 0, y = 0;
short diff, result;
if(a > 0) y = 1;
if(b > 0) x = 1;
diff = the_image[y+i+A*ROWS/factor][x+j+B*COLS/factor] -
the_image[i+A*ROWS/factor][j+B*COLS/factor];
/***********************************************
*
* If you are at the edge of the input image
* array, then you cannot interpolate to the
* next point because there is no next point.
* Therefore, set the diff to 0.
*
***********************************************/
if((y+i+A*ROWS/factor) >= ROWS) diff = 0;
if((x+j+B*COLS/factor) >= COLS) diff = 0;
num = a+b;
if(num > factor) num = factor;
result = the_image[i+A*ROWS/factor][j+B*COLS/factor] +
num*diff/factor;
return(res